<?php

namespace App\Services;


use App\Models\Cms_Menu;
use App\Models\Sys_Permission;
use App\Models\Sys_Role;
use App\Traits\ResponseApi;

class RoleService
{
    public function getRoleList($params)
    {
        return Sys_Role::getFilter($params)->get();
    }

    public function getList($params)
    {
        $result = Sys_Role::getFilter($params)->Nodel()->paginate($params['pageSize']);
        foreach ($result as $key => $value){
            $result[$key]['menu_ids'] = json_decode($value->menu_ids,true);
        }
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return $list;
    }

    public function addRole($params)
    {
//        print_r(json_encode($params['menu_ids']));exit;
        $params['menu_ids'] = json_encode($params['menu_ids']);
        if(isset($params['id']) && !empty($params['id'])){
            $res = Sys_Role::where('id',$params['id'])->update($params);
            if($res)
                return true;
            else
                return false;
        }else{
            $res = Sys_Role::create($params);
            if($res)
                return true;
            else
                return false;
        }
    }

    public function delRole($params)
    {
        $str = strstr((string)$params['ids'],',');

        if($str){
            $ids = explode(',',$params['ids']);
            $res = Sys_Role::whereIn('id',$ids)->update(['is_del' => 'Y']);
        }else{
            $res = Sys_Role::where('id',$params['ids'])->update(['is_del' => 'Y']);
        }

        return ['status' => true,'code' => 200,'data' => $res];
    }

}
